﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SimpleForm
{
    public partial class FormShow : Form
    {
        public FormShow()
        {
            InitializeComponent();
        }

        private string userName = "You";
        private string aiName = "JJMoe";

        private bool IfAllPattern(string[] tokens, params string[] mustHaves)
        {
            return mustHaves.All(s => tokens.Contains(s));
        }

        private bool IfAnyPattern(string[] tokens, params string[] mustHaves)
        {
            return mustHaves.Any(s => tokens.Contains(s));
        }

        private string AiAnswer(string inputString)
        {
            inputString = inputString.ToLower();
            var tokens = inputString
                .Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                .Select(s => s.ToLower())
                .ToArray()
                ;
            if (IfAllPattern(tokens, "who", "you"))
            {
                return "I am AI JJMoe!";
            }
            else if (IfAnyPattern(tokens, "hello", "hi"))
            {
                return "Hi~";
            }
            else if (IfAllPattern(tokens, "what", "date", "today"))
            {
                return "Today is " + System.DateTime.Today.ToLongDateString();
            }
            else if (IfAnyPattern(tokens, "bye", "88", "3166"))
            {
                return "Good bye! See you next time!";
            }
            else
            {
                return "Sorry, JJMoe doesn't know what you mean. Please teach me!";
            }
        }

        private void UserSendSomething()
        {
            string outputString = userName + ": ";
            string inputString = "";
            if (textBoxInput.Lines.Count() > 0)
            {
                foreach (var singleLine in textBoxInput.Lines)
                {
                    inputString += singleLine;
                }

                // Show user's message.
                textBoxShow.AppendText(userName + ": " + inputString + "\n\n");
                textBoxInput.Clear();

                // AI answer the question.
                textBoxShow.AppendText(aiName + ": " + AiAnswer(inputString) + "\n\n");

                if (TestSegmentor.bIsDebugging)
                {
                    // Prints debug string.
                    Segmentor segmentor = new Segmentor(inputString, LANGUAGE.CHS);
                    var list = segmentor.SegmentedList;
                    textBoxShow.AppendText(segmentor.DebugString);
                }
            }
            else
            {
                MessageBox.Show("Input something before you send.");
            }
        }
        private void buttonSend_Click(object sender, EventArgs e)
        {
            UserSendSomething();
        }

        private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                UserSendSomething();
                e.Handled = true;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (buttonCancel.Text == "Enable Debug")
                buttonCancel.Text = "Disable Debug";
            else
                buttonCancel.Text = "Enable Debug";
            TestSegmentor.bIsDebugging = TestSegmentor.bIsDebugging ? false : true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
                string intputString = "hello world and java.";
                Segmentor enSegmentor = new Segmentor(intputString);
                var list = enSegmentor.SegmentedList;
            }

            {
                string intputString = "操作系统，黑长直，囧";
                Segmentor chsSegmentor = new Segmentor(intputString, LANGUAGE.CHS);
                var list = chsSegmentor.SegmentedList;
            }
        }
    }
}
